# - 1 登陆
# 导包
import requests


# 登录

response = requests.post(url= " http://ihrm-test.itheima.net/api/sys/login",
                         headers={"Content-Type":"application/vnd.api+json"},
                         json={"mobile": "13800000002", "password": "123456"})
print("登陆的响应数据为：", response.json())
# - 2 提取登陆的令牌，保存到全局变量
token = response.json().get("data")
print("提取的token为：", token)
g_token = "Bearer " + token
print("令牌保存到全局变量请求头之后的结果为：", g_token)

# cookie = response.cookies

# 部门列表查询
response = requests.get(url= "http://ihrm-test.itheima.net/api/company/department",
                        )

print(("部门列表的结果为：", response.json()))
# 添加部门
response = requests.post(url= "http://ihrm-test.itheima.net/api/company/department",
                         headers={"Content-Type": "g_token"},
                         json={"name":"软件测试","code":"9527"})
print("添加部门结果为：", response.json())

# 提取部门的ID
EMP_ID=response.json().get("data").get("id")
print(("提取并保存到全局变量的部门ID为：", EMP_ID))

# 查询部门
response = requests.get(url= "http://ihrm-test.itheima.net/api/company/department"+"/"+ EMP_ID,
                        headers={"Content-Type": "g_token"})

print(("查询员工的URL为：", response.url))
print(("查询员工的URL为：", response.json()))

# 部门修改
response = requests.put(url= "http://ihrm-test.itheima.net/api/company/department/:id"+"/"+ EMP_ID,
                        headers={"Content-Type": "g_token"},
                        json={"name":"软件测试","code":"1222"})
print("修改部门的URL为：", response.url)
print("修改部门的结果为：", response.json())

# 删除部门
response = requests.delete(url= "http://ihrm-test.itheima.net/api/company/department/:id"+"/"+ EMP_ID,
                           headers={"Content-Type": "g_token"})
print(("查询员工的URL为：", response.url))
print(("查询员工的URL为：", response.json()))